package com.spring.redis.operation.service.base.impl;

import com.spring.redis.operation.service.base.HashRedisBaseService;
import com.spring.redis.operation.utils.RedisOperationUtils;
import lombok.NonNull;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * redis Operation hash class
 *
 * @author zhouyu
 */
@Service
public class HashRedisBaseServiceImpl extends RedisBaseServiceImpl implements HashRedisBaseService {

    private final HashOperations<String, String, Object> hashOperations;

    public HashRedisBaseServiceImpl(RedisTemplate<String, Object> redisTemplate, HashOperations<String, String, Object> hashOperations) {
        super(redisTemplate);
        this.hashOperations = hashOperations;
    }

    /**
     * 存储hash类型的值
     *
     * @param key   hash表的名称
     * @param field field
     * @param value value
     */
    @Override
    public void hSet(@NonNull String key, @NonNull String field, @NonNull Object value) {
        hashOperations.put(key, field, value);
    }

    /**
     * 批量存储hash类型的值
     *
     * @param key  hash表的名称
     * @param hash hash
     */
    @Override
    public void hMSet(@NonNull String key, @NonNull Map<String, Object> hash) {
        hashOperations.putAll(key, hash);
    }

    /**
     * 获取hash类型的值
     *
     * @param key   key
     * @param field filed
     * @return java.lang.String
     */
    @Override
    public String hGet(@NonNull String key, @NonNull String field) {
        Object v = hashOperations.get(key, field);
        return null == v ? null : v.toString();
    }

    /**
     * 批量获取hash类型的值
     *
     * @param key    key
     * @param fields fields
     * @return java.util.List<String>
     */
    @Override
    public List<Object> hMGet(@NonNull String key, @NonNull String... fields) {
        return hashOperations.multiGet(key, RedisOperationUtils.arrToSet(fields));
    }

    /**
     * 删除hash类型的值
     *
     * @param key    key
     * @param fields fields
     */
    @Override
    public void hDel(@NonNull String key, @NonNull Object... fields) {
        hashOperations.delete(key, fields);
    }

    /**
     * 判断hash表中是否有该项的值
     *
     * @param key  键 不能为null
     * @param item 项 不能为null
     * @return true 存在 false不存在
     */
    @Override
    public boolean hHasKey(String key, String item) {
        return hashOperations.hasKey(key, item);
    }

    /**
     * 获取hash的所有key
     *
     * @param key key
     * @return Set
     */
    @Override
    public Set<String> hKeys(@NonNull String key) {
        return hashOperations.keys(key);
    }

    /**
     * 获取hash的所有field和value
     *
     * @param key key
     * @return java.util.Map<String, Object>
     */
    @Override
    public Map<String, Object> hGetAll(@NonNull String key) {
        return hashOperations.entries(key);
    }

    /**
     * 获取hash的所有value
     *
     * @param key key
     * @return java.util.List<Object>
     */
    @Override
    public List<Object> hValues(@NonNull String key) {
        return hashOperations.values(key);
    }

    /**
     * hash递增 如果不存在,就会创建一个 并把新增后的值返回
     *
     * @param key  键
     * @param item 项
     * @param by   递增递减值
     * @return long
     */
    @Override
    public long hIncr(String key, String item, long by) {
        return hashOperations.increment(key, item, by);
    }

    /**
     * 获取map长度
     *
     * @param key key
     * @return
     */
    @Override
    public long size(String key) {
        return hashOperations.size(key);
    }

}
